import * as path from 'path';
import * as webpack from 'webpack';

const tsImportPluginFactory = require('ts-import-plugin');
const bundlePath = path.resolve(__dirname, "dist/");
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");

const BaseConfig: webpack.Configuration = {
  entry: {
    bundle: "./src/index.tsx"
  },
  module: {
    rules: [
      {
        test: /\.(jsx|tsx|js|ts)$/,
        loader: 'ts-loader',
        options: {
          transpileOnly: true,
          experimentalWatchApi: true,
          getCustomTransformers: () => ({
            before: [tsImportPluginFactory({
              libraryName: 'antd',
              libraryDirectory: 'lib',
              style: 'css'
            })]
          }),
          compilerOptions: {
            module: 'es2015'
          }
        },
        exclude: /node_modules/
      },
      {
        enforce: "pre",
        test: /\.js$/,
        loader: "source-map-loader",
        exclude: [
          path.resolve(path.join(__dirname, 'node_modules/mutationobserver-shim'))
        ]
      },
      {
        enforce: 'pre',
        test: /\.tsx?$/,
        use: "source-map-loader",
        exclude: [
          path.resolve(path.join(__dirname, 'node_modules/mutationobserver-shim'))
        ]
      },
      {
        test: /\.css$/,
        use: [MiniCssExtractPlugin.loader, 'css-loader']
      },
      {
        test: /\.less$/,
        use: [
          MiniCssExtractPlugin.loader, 'css-loader', 'less-loader'
        ]
      },
      {
        test: /\.(png|jpg|gif)$/i,
        use: [
          {
            loader: 'url-loader',
            options: {
              limit: 8192
            }
          }
        ]
      }
    ]
  },
  resolve: {
    extensions: [".ts", ".tsx", ".js", ".json"]
  },
  output: {
    path: bundlePath,
    filename: 'js/[name].js',
    chunkFilename: 'js/[name].[chunkhash:8].js'
  },
  optimization: {
    splitChunks: {
      chunks: 'initial',
      cacheGroups: {
        avendor: {
          test: /node_modules\/a(.*)\//,
          name: 'a-vendor',
          priority: 0,
          enforce: true
        },
        bvendor: {
          test: /node_modules\/b(.*)\//,
          name: 'b-vendor',
          priority: 0,
          enforce: true
        },
        cvendor: {
          test: /node_modules\/c(.*)\//,
          name: 'c-vendor',
          priority: 0,
          enforce: true
        },
        rvendor: {
          test: /node_modules\/r(.*)\//,
          name: 'r-vendor',
          priority: 0,
          enforce: true
        },
        commons: {
          test: /common\/|components\//,
          name: 'commons',
          priority: -5,
          enforce: true
        }
      }
    },
    runtimeChunk: {
      name: 'manifest'
    },
    minimizer: [
      new UglifyJsPlugin({
        cache: true,
        parallel: true,
        sourceMap: false
      }),
      new OptimizeCSSAssetsPlugin({})
    ]
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: "css/[name].css",
      chunkFilename: "css/[name].[hash].css"
    })
  ]
};

export default BaseConfig;